package ru.stepuk.modern.gui;

import com.sun.corba.se.impl.ior.iiop.JavaSerializationComponent;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import ru.stepuk.modern.Path;
import ru.stepuk.modern.nodes.Anchor;
import ru.stepuk.modern.nodes.Node;
import ru.stepuk.modern.nodes.Source;

/**
 *
 * @author ivan
 */
public class PopupMenus {

    public static interface Updateable {

        void update(VisualizationViewer<Node, Path> vv, Node node, Path path);
    }

    public static class NodeMenu extends JPopupMenu implements Updateable {

        private VisualizationViewer vv;
        private Node node;
        private JMenuItem props;
        private JMenuItem rename;
        private JMenuItem delete;
        private JMenuItem popul;

        public NodeMenu() {

            props = add(new JMenuItem("Свойства…",
                    new ImageIcon(getClass().getResource("/images/property.png"))));

            popul = add(new JMenuItem("Количество требований…",
                    new ImageIcon(getClass().getResource("/images/reports-stack.png"))));

            rename = add(new JMenuItem("Переименовать…",
                    new ImageIcon(getClass().getResource("/images/pencil.png"))));
            add(new Separator());
            delete = add(new JMenuItem("Удалить элемент",
                    new ImageIcon(getClass().getResource("/images/cross.png"))));

            rename.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    new RenameNodeDialog(node, vv).setVisible(true);

                }
            });



            props.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    node.getPropertiesDialog().setVisible(true);

                }
            });

            delete.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    vv.getGraphLayout().getGraph().removeVertex(node);
                    vv.repaint();
                }
            });
        }

        public void update(VisualizationViewer<Node, Path> vv, Node node, Path path) {
            this.vv = vv;
            this.node = node;

            //TODO костыли
            props.setVisible(node.getPropertiesDialog() != null);
            popul.setVisible(false);
            if (node instanceof Source) {

                popul.setVisible(true);
                if (popul.getActionListeners().length > 0) {
                    popul.removeActionListener(popul.getActionListeners()[0]);
                }
                popul.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        ((Source) NodeMenu.this.node).getPopulationDialog().setVisible(true);

                    }
                });
            }
        }
    }

    public static class PathMenu extends JPopupMenu implements Updateable {

        private VisualizationViewer vv;
        private Path path;

        public PathMenu() {

            JMenuItem setweight = add(new JMenuItem("Задать вес…",
                    new ImageIcon(getClass().getResource("/images/tag.png"))));

            add(new JSeparator());



            JMenuItem delete = add(new JMenuItem("Удалить переход",
                    new ImageIcon(getClass().getResource("/images/cross.png"))));
            delete.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    vv.getGraphLayout().getGraph().removeEdge(path);
                    vv.repaint();
                }
            });


            setweight.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    (new PathWeightDialog(path, vv)).setVisible(true);
                }
            });
        }

        public void update(VisualizationViewer<Node, Path> vv, Node node, Path path) {
            this.vv = vv;
            this.path = path;
        }
    }
}
